package com.standardoperationapp.operationplan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import com.standardoperationapp.R;
import com.standardoperationapp.adapter.PlanListAdapter;
import com.standardoperationapp.bean.OperationPlanBean;
import com.standardoperationapp.global.GsonUtils;
import com.standardoperationapp.materialinventory.activity.ListItemActivity;
import com.standardoperationapp.net.OnSuccessAndFaultListener;
import com.standardoperationapp.net.OnSuccessAndFaultSub;
import com.standardoperationapp.net.OperationApi;
import com.standardoperationapp.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作业计划
 */
public class OperationPlanActivity extends AppCompatActivity {

    private ListView listView;
    private PlanListAdapter adapter;
    private List<OperationPlanBean.DataBean> dataBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_operation_plan);
        listView = (ListView) findViewById(R.id.planlist);
        dataBeanList = new ArrayList<>();
        adapter = new PlanListAdapter(this, dataBeanList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(OperationPlanActivity.this, PlanPointListActivity.class);
                intent.putExtra("planId", dataBeanList.get(i).getPlanId());
                startActivity(intent);
            }
        });

        initData();
    }

    public void initData() {
        OperationApi.selectWorkPlanListByid(new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
                OperationPlanBean bean = GsonUtils.fromJson(result.toString(), OperationPlanBean.class);
                dataBeanList.clear();
                if (bean.getData() != null) {
                    dataBeanList.addAll( bean.getData());
                }
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onFault(String errorMsg) {
                ToastUtil.showToast(errorMsg);
            }
        }));


    }
}